<?php

namespace Common\Model;

class UserModel extends BaseModel {

  var $userObj;
  var $csvField = array('nice_name', 'area', 'phone', 'email', 'password', 'sex', 'zipcode', 'address', 'bank_name', 'bank_account', 'bank_address', 'pay_id', 'code', 'code_img', 'supplier_img');
  var $sex = array(0 => '女', 1 => '男');
  var $userType = array(1 => '新用户', 2 => '软件用户', 3 => '代理商');
  var $checkSupplier = array('-1' => '驳回', 0 => '未提交', 1 => '未审核', 2 => '已审核');
  var $applayStatus = array('-1' => '驳回', 0 => '未提交', 1 => '待审核', 2 => '已审核');
  var $userMenu = array(
      '1' => array('User_index'),
      '2' => array('User_index', 'User_supplier'),
      '3' => array('User_index', 'User_child', 'User_ahref'),
  );

  public function __construct($name = '', $tablePrefix = '', $connection = '') {
    parent::__construct($name, $tablePrefix, $connection);
    $this->userObj = M("User");
  }

  /**
   * 添加会员
   * @param array $data 会员信息
   * @return id 会员编号
   */
  public function addUser($data) {
    $data['add_time'] = time();
    $this->userObj->create($data);
    return $this->userObj->add();
  }

  /**
   * 删除会员
   * @param array $id 会员编号
   */
  public function delUser($id) {
    $data['id'] = $id;
    $this->userObj->where($data)->delete();
    return true;
  }

  /**
   * 更新会员
   * @param array $data 会员信息
   * @param array $id 会员编号
   */
  public function updateUser($data, $id) {
    $where['id'] = $id;
    $this->userObj->where($where)->save($data);
  }

  /**
   * 查询会员信息
   * @param array $data 查询条件
   */
  public function userInfo($data) {
    $userInfo = $this->userObj->where($data)->find();
    $userInfo = $this->userItem($userInfo);
    return $userInfo;
  }

  /**
   * 查询会员列表(有分页)
   * @param array $where 搜索条件
   * @return array 列表 分页
   */
  public function userList($where, $last = 0) {
    $count = $this->userObj->where($where)->count();
    $pageInfo = $this->getPageInfo($last, $count);
    $list = $this->userObj->where($where)->order('id desc')->limit($pageInfo['last'] . ',' . $pageInfo['listNum'])->select();
    return array('list' => $list, 'page' => $pageInfo['page']);
  }

  /**
   * 处理用户字段数据
   * @param array $userInfo 用户详情
   * @return array 用户详情
   */
  private function userItem($userInfo) {
    $userInfo['username'] = date('Y-m-d', $userInfo['username']);
    $userInfo['userTypeName'] = $this->userType[$userInfo['user_type']];
    $userInfo['parentInfo'] = $this->userObj->where(array('id' => $userInfo['parent_id']))->find();
    $userInfo['checkSupplierName'] = $this->checkSupplier[$userInfo['check_supplier']];
    $userInfo['applayStatusName'] = $this->applayStatus[$userInfo['applay_status']];
    $userInfo['applayInfo'] = unserialize($userInfo['applay_info']);
    $userInfo['applayInfo']['add_time'] = date('Y-m-d', $userInfo['applayInfo']['add_time']);
    $userInfo['supplier_time'] = date('Y-m-d', $userInfo['supplier_time']);
    return $userInfo;
  }

  /**
   * 获取下线列表
   * @param type $id 上线编号
   * @param type $lv 层级
   * @param int $i 计数
   * @return type 下线编号
   */
  public function userChildList($id, $lv = 1, $i = 0) {
    $i += 1;
    $where = array('parent_id' => $id);
    $list = $this->userObj->where($where)->select();
    foreach ($list as $item) {
      $res['idArray'][$item['id']] = $item['id'];
      $res['infoArray'][$item['id']] = $item;
      if ($item['id'] > 0 && ($i < $lv || $lv == 0)) {
        $childArray = $this->userChildList($item['id'], $lv, $i);
        if (!empty($childArray)) {
          $res['idArray'] = @$res['idArray'] + $childArray['idArray'];
          $res['infoArray'][$item['id']]['child'] = $childArray['infoArray'];
        }
      }
    }
    $res['childCount'] = count($res['idArray']);
    return $res;
  }

  /**
   * 根据名称获取用户编号
   */
  public function getUserIdByName($where) {
    $ids = $this->userObj->where($where)->getField('GROUP_CONCAT( CAST(  `id` AS CHAR ) ) AS ids');
    return array('ids' => $ids);
  }

}
